package es.ibm.santillana.mdm.defaultvaluerule;

import com.ibm.pim.catalog.Catalog;
import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.context.Context;
import com.ibm.pim.context.PIMContextFactory;
import com.ibm.pim.extensionpoints.CategoryDefaultValueRuleFunctionArguments;
import com.ibm.pim.extensionpoints.CollaborationCategoryDefaultValueRuleFunctionArguments;
import com.ibm.pim.extensionpoints.CollaborationItemDefaultValueRuleFunctionArguments;
import com.ibm.pim.extensionpoints.DefaultValueRuleFunction;
import com.ibm.pim.extensionpoints.ItemDefaultValueRuleFunctionArguments;
import com.ibm.pim.lookuptable.LookupTable;
import com.ibm.pim.lookuptable.LookupTableEntry;
import com.ibm.pim.lookuptable.LookupTableManager;

import es.ibm.santillana.mdm.common.util.ConstSantillana;

public class DefaultValuesRule implements DefaultValueRuleFunction {

	@Override
	public Object rule(ItemDefaultValueRuleFunctionArguments arg0) {
		Context ctxPIM = PIMContextFactory.getCurrentContext();
		LookupTable lkpDeafultValues = ctxPIM.getLookupTableManager().getLookupTable(ConstSantillana.LOOKUP_DEFAULT_VALUES);
		String attName = arg0.getAttributeInstance().getAttributeDefinition().getName();
		System.out.println("name Default Attribute "+attName);
		String defValue= "";
		LookupTableEntry lkpEntry =lkpDeafultValues.getLookupTableEntry(attName);
		String ctgName = (String) lkpEntry.getAttributeValue("Default Values LTS/Catalog");
		System.out.println("ctgName "+ctgName);
		String pkDefault = (String) lkpEntry.getAttributeValue("Default Values LTS/PK");
		System.out.println("pkDefault "+pkDefault);
		
		
		Catalog ctg = ctxPIM.getCatalogManager().getCatalog(ctgName);
		if (ctg != null){
			String spcName = ctg.getSpec().getName();
			Item defItem = ctg.getItemByPrimaryKey(pkDefault);
			if (defItem != null){
				defValue = (String) defItem.getAttributeValue(spcName +"/Valor/es_ES");
			}
		}
		return defValue;
	}

	@Override
	public Object rule(CollaborationItemDefaultValueRuleFunctionArguments arg0) {
		Context ctxPIM = PIMContextFactory.getCurrentContext();
		LookupTable lkpDeafultValues = ctxPIM.getLookupTableManager().getLookupTable(ConstSantillana.LOOKUP_DEFAULT_VALUES);
		String attName = arg0.getAttributeInstance().getAttributeDefinition().getName();
		System.out.println("name Default Attribute "+attName);
		String defValue= "";
		LookupTableEntry lkpEntry =lkpDeafultValues.getLookupTableEntry(attName);
		String ctgName = (String) lkpEntry.getAttributeValue("Default Values LTS/Catalog");
		System.out.println("ctgName "+ctgName);
		String pkDefault = (String) lkpEntry.getAttributeValue("Default Values LTS/PK");
		System.out.println("pkDefault "+pkDefault);
		
		
		Catalog ctg = ctxPIM.getCatalogManager().getCatalog(ctgName);
		if (ctg != null){
			String spcName = ctg.getSpec().getName();
			Item defItem = ctg.getItemByPrimaryKey(pkDefault);
			if (defItem != null){
				defValue = (String) defItem.getAttributeValue(spcName +"/Valor/es_ES");
			}
		}
		return defValue;
	}

	@Override
	public Object rule(CategoryDefaultValueRuleFunctionArguments arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object rule(
			CollaborationCategoryDefaultValueRuleFunctionArguments arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
